public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (p.val > q.val) {
            TreeNode temp = p;
            p = q;
            q = temp;
        }

        TreeNode cur = root;
        while (true) {
            if (p.val <= cur.val && q.val >= cur.val) {
                return cur;
            }
            if (cur.val < p.val) {
                cur = cur.right;
            } else {
                cur = cur.left;
            }
        }
    }
}